
package com.controller;

import com.domain.Admin;
import com.service.IManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@SessionAttributes({"admin"})
@Controller
@RequestMapping({"/admin"})
public class AdminController {

    @Autowired
    /*    */ private IManagerService managerService;

    /*    */
    /*    */
    @RequestMapping({"/findAllAdmin"})
    /*    */ public String findAll(Model model) throws Exception {
        /* 31 */
        List<Admin> adminList = this.managerService.getAllAdmin();
        /* 32 */
        model.addAttribute("admins", adminList);
        /* 33 */
        return "/admin/admin_all";
        /*    */
    }

    /*    */
    /*    */
    /*    */
    /*    */
    @RequestMapping({"/addAdmin"})
    /*    */ public ModelAndView addAdmin(@RequestParam("id") Integer id, @RequestParam("name") String name, @RequestParam("password") String password) {
        /* 40 */
        List<Admin> admins = this.managerService.getByName(id);
        /* 41 */
        ModelAndView modelAndView = new ModelAndView();
        /* 42 */
        Admin admin = new Admin();
        /* 43 */
        admin.setId(id);
        /* 44 */
        admin.setName(name);
        /* 45 */
        admin.setPassword(password);
        /* 46 */
        if (admins.size() == 0) {
            /* 47 */
            int result = this.managerService.addAdmin(admin);
            /* 48 */
            modelAndView.setViewName("redirect:/admin/findAllAdmin");
            /* 49 */
            return modelAndView;
            /*    */
        }
        /* 51 */
        modelAndView.addObject("admin", admin);
        /* 52 */
        modelAndView.addObject("msg", "管理员账号不能重复!!!");
        /* 53 */
        modelAndView.setViewName("/admin/admin-add-error");
        /* 54 */
        return modelAndView;
        /*    */
    }

    /*    */
    /*    */
    /*    */
    /*    */
    /*    */
    /*    */
    /*    */
    /*    */
    @RequestMapping({"/toUpdateAdmin"})
    /*    */ public String updateAdmin(@RequestParam("id") Integer id, Model model) {
        /* 65 */
        Admin admin = this.managerService.getAdminById(id);
        /* 66 */
        model.addAttribute("admin", admin);
        /* 67 */
        return "/admin/admin-update";
        /*    */
    }

    /*    */
    /*    */
    /*    */
    /*    */
    @RequestMapping({"/updateAdmin"})
    /*    */ public ModelAndView updateAdmin(@RequestParam("id") Integer id, @RequestParam("name") String name, @RequestParam("password") String password) {
        /* 74 */
        ModelAndView modelAndView = new ModelAndView();
        /* 75 */
        Admin admin = new Admin();
        /* 76 */
        admin.setId(id);
        /* 77 */
        admin.setName(name);
        /* 78 */
        admin.setPassword(password);
        /* 79 */
        int result = this.managerService.updateAdmin(admin);
        /* 80 */
        modelAndView.setViewName("redirect:/admin/findAllAdmin");
        /* 81 */
        return modelAndView;
        /*    */
    }

    /*    */
    @RequestMapping({"/deleteAdmin"})
    /*    */ public ModelAndView deleteAdmin(@RequestParam("id") Integer id) {
        /* 85 */
        ModelAndView modelAndView = new ModelAndView();
        /* 86 */
        int result = this.managerService.deleteAdmin(id);
        /* 87 */
        modelAndView.setViewName("redirect:/admin/findAllAdmin");
        /* 88 */
        return modelAndView;
    }
}

